package io.gitee.zhucan123.core.infrastructure.exception.advice;

import com.niimbot.middlend.core.infrastructure.exception.DuplicateNameException;
import com.niimbot.middlend.core.infrastructure.exception.GeneratorCodeException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: homo
 * @description:
 * @author: xuqigao
 * @date: 2019-05-21 14:15
 */
@RestControllerAdvice
public class GeneratorCodeExceptionAdvice {
  @ResponseBody
  @ExceptionHandler(GeneratorCodeException.class)
  @ResponseStatus(HttpStatus.NOT_FOUND)
  ErrorDTO codeNotFoundException(HttpServletRequest req, DuplicateNameException ex) {
    String errorMessage = ex.getMessage();
    String errorURL = req.getRequestURL().toString();
    return new ErrorDTO(errorMessage, HttpStatus.NOT_FOUND.value(), errorURL);
  }
}
